package com.zp.business.controller.patPrepayment;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.patPrepayment.bo.PatPrepaymentBo;
import com.zp.business.entity.patPrepayment.vo.PatPrepaymentVo;
import com.zp.business.service.patPrepayment.IPatPrepaymentService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 患者住院预交金控制器
 * 前端访问路由地址为:/business/patPrepayment
 *
 * @author zhang peng
 * @ date 2024-07-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patPrepayment")
public class PatPrepaymentController extends BaseController {

    private final IPatPrepaymentService iPatPrepaymentService;

    /**
     * 查询患者住院预交金列表 (分页)
     */
    @SaCheckPermission("finance:patPrepayment:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PatPrepaymentVo> selectPage(PatPrepaymentBo bo, PageQuery pageQuery) {
        return iPatPrepaymentService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询患者住院预交金列表 (不分页)
     */
    @SaCheckPermission("finance:patPrepayment:list")
    @GetMapping("/list")
    public R<List<PatPrepaymentVo>> list(PatPrepaymentBo bo) {
        List<PatPrepaymentVo> list = iPatPrepaymentService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出患者住院预交金列表
     */
    @SaCheckPermission("finance:patPrepayment:export")
    @Log(title = "患者住院预交金", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatPrepaymentBo bo, HttpServletResponse response) {
        List<PatPrepaymentVo> list = iPatPrepaymentService.queryList(bo);
        ExcelUtil.exportExcel(list, "患者住院预交金", PatPrepaymentVo.class, response);
    }

    /**
     * 获取患者住院预交金详细信息
     *
     * @param prepaymentId 主键
     */
    @SaCheckPermission("finance:patPrepayment:list")
    @GetMapping("/{prepaymentId}")
    public R<PatPrepaymentVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String prepaymentId) {
        return R.ok(iPatPrepaymentService.queryById(prepaymentId));
    }

    /**
     * 新增患者住院预交金
     */
    @SaCheckPermission("finance:patPrepayment:add")
    @Log(title = "患者住院预交金", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatPrepaymentBo bo) {
        return toAjax(iPatPrepaymentService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改患者住院预交金
     */
    @SaCheckPermission("finance:patPrepayment:edit")
    @Log(title = "患者住院预交金", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatPrepaymentBo bo) {
        return toAjax(iPatPrepaymentService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除患者住院预交金
     *
     * @param prepaymentIds 主键串
     */
    @SaCheckPermission("finance:patPrepayment:remove")
    @Log(title = "患者住院预交金", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prepaymentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] prepaymentIds) {
        return toAjax(iPatPrepaymentService.deleteWithValidByIds(Arrays.asList(prepaymentIds), true) ? 1 : 0);
    }

    /**
     * 预交金退费、作废
     *
     * @param prepaymentIds 主键串
     */
    @SaCheckPermission("finance:patPrepayment:remove")
    @Log(title = "预交金退费、作废", businessType = BusinessType.DELETE)
    @DeleteMapping("/refundApplication/{prepaymentId}/{type}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String prepaymentId,
                          @NotEmpty(message = "操作类型不能为空") @PathVariable String type) {
        return toAjax(iPatPrepaymentService.refundApplication(prepaymentId, type) ? 1 : 0);
    }
}
